import { client } from '../config/index'
import { nanoid } from 'nanoid'

const tool = {
  oss: {
    async upload(file: any) {
      try {
        // 文件名
        const uuid = nanoid()
        // 文件后缀名
        const index = file.name.lastIndexOf('.')
        const suffix = file.name.substr(index)
        // 完整路径
        const fileName = `${uuid}${suffix}`

        const result = await client.multipartUpload(fileName, file, {
          progress: function (p) {
            console.log(p)
          }
        })
        console.log(result, '上传成功')
        return result
      } catch (error) {                                                                
        console.error('上传失败:', error)
        throw error // 抛出错误，可以在调用处捕获处理
      }
    }
  }
}

export default tool
